package com.college.service.impl;

import com.college.entity.*;
import com.college.mapper.*;
import com.college.service.TeacherInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.teacherDTO.TeachInfoDTO;
import com.college.teacherDTO.TeacherPageOneDTO;
import com.college.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 教师信息 服务实现类
 * </p>
 *
 * @author yoki
 * @since 2024-05-29
 */
@Service
public class TeacherInfoServiceImpl extends ServiceImpl<TeacherInfoMapper, TeacherInfo> implements TeacherInfoService {

    @Autowired
    private TeacherInfoMapper teacherInfoMapper;

    @Autowired
    private SchoolCalendarInfoMapper schoolCalendarInfoMapper;

    @Autowired
    private MajorInfoMapper majorInfoMapper;

    @Autowired
    private DepartmentInfoMapper departmentInfoMapper;

    @Autowired
    private ScheduleInfoMapper scheduleInfoMapper;

    @Autowired
    private ClassInfoMapper classInfoMapper;

    @Autowired
    private CourseInfoMapper courseInfoMapper;

    @Autowired
    private StudentInfoMapper studentInfoMapper;

    //自定义方法，查找出有最多课程的学期，输出该学期的课程列表
    public List<ScheduleInfo> getMax(List<ScheduleInfo> scheduleInfos)
    {
        int Max = 0;
        int number = 0;
        int calendarId =0;
        for(int i=0;i<scheduleInfos.size();i++)
        {
            for (int j=0;j<scheduleInfos.size();j++)
            {
                if (scheduleInfos.get(i).getCalendarId()==scheduleInfos.get(j).getCalendarId())
                {
                    number++;
                }
            }
            if(number>Max)
            {
                Max=number;
                calendarId=scheduleInfos.get(i).getCalendarId();
            }
            number=0;
        }
        for(int n=0;n<scheduleInfos.size();n++)
        {
            if (calendarId!=scheduleInfos.get(n).getCalendarId())
            {
                scheduleInfos.remove(n);
            }
        }
        return scheduleInfos;
    }

    public long dayDifference(Date beginTime,Date endTime)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.format(beginTime);
        dateFormat.format(endTime);
        long msNum = endTime.getTime()-beginTime.getTime();//时间戳相差的毫秒数
        long dayNum = msNum/(24*60*60*1000);//除以一天的毫秒数，得到相差天数
        return dayNum;
    }

    /**
     * 教师第一页接口实现
     * 启动页
     *
     * @param id
     * @return 该页面展示教师名称，教师职位，教师职称
     */
    @Override
    public R<TeacherPageOneDTO> startPage(Integer id)
    {
        TeacherInfo teacherInfo =  teacherInfoMapper.getById(id);
        //用该教师的入职时间获取入职学期的校历
        SchoolCalendarInfo calendarInfo1 =  schoolCalendarInfoMapper.getNowCalendar(teacherInfo.getEmploymentTime());
        //获取当前时间的校历
        SchoolCalendarInfo calendarInfo2 = schoolCalendarInfoMapper.getNowCalendar(new Date());
        //用当前学期校历的结束时间-去入职时间
        long diffDays = dayDifference(teacherInfo.getEmploymentTime(),calendarInfo2.getEndTime());
        int days = (int)diffDays;
        //若两对象id不一样，则该老师不是在该学期入的职
        if(calendarInfo2.getId()!=calendarInfo1.getId())
        {
            //判断是否在同一学年
            if(calendarInfo1.getSchoolYear().equals(calendarInfo2.getSchoolYear()))
            {
                days-=30;
            }
            else
            {
                //若不是在同一学年，先用学年字段进行减法计算
                //切割字符串，计算相差年数
                String endYear = calendarInfo2.getSchoolYear().substring(0,4);
                String beginYear = calendarInfo1.getSchoolYear().substring(0,4);
                Integer years = Integer.parseInt(endYear)-Integer.parseInt(beginYear);
                //若相隔n年，则必有n个暑假和n-1个寒假
                days-=days-(years*60+(years-1)*30);
                //最后判断入职学期和现在学期是是上半学期还是下
                //若入职是在上半学期，则再减去一个寒假
                if (calendarInfo1.getSemester().equals("1"))
                {
                    days-=30;
                }
                //若现在校历是在下半学期，也要再减去一个寒假
                if (calendarInfo2.getSemester().equals("2"))
                {
                    days-=30;
                }
            }
        }
        TeacherPageOneDTO teacherPageOneDTO = new TeacherPageOneDTO();
        switch (teacherInfo.getPosition())
        {
            case 1:
                teacherPageOneDTO.setPositionName("教授");
                break;
            case 2:
                teacherPageOneDTO.setPositionName("副教授");
                break;
            case 3:
                teacherPageOneDTO.setPositionName("讲师");
                break;
            case 4:
                teacherPageOneDTO.setPositionName("助教");
                break;
        }
        teacherPageOneDTO.setDays(days);
        teacherPageOneDTO.setTeacherName(teacherInfo.getTeacherName());
//        teacherPageOneDTO.setPosition(teacherInfo.getPosition());
        return R.success(teacherPageOneDTO);
    }

    /**
     * 教师第二页接口实现
     * 我和重工页
     * @param id
     * @return
     * //该页面展示教师入职时间
     */
    @Override
    public R<String> meAndCG(Integer id)
    {
        TeacherInfo teacherInfo =  teacherInfoMapper.getById(id);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String EmDate  = dateFormat.format(teacherInfo.getEmploymentTime());
        EmDate = (EmDate.substring(0,4)+"年"+EmDate.substring(5,7)+"月"+EmDate.substring(8)+"日");
        return R.success(EmDate);
    }

    /**
     * 教师第三页接口实现
     * 档案信息页
     * @param id
     * @return
     * //该页面展示教师所在专业即学院
     */
    @Override
    public R<String> archivesInfo(Integer id)
    {
        TeacherInfo teacherInfo =  teacherInfoMapper.getById(id);
        MajorInfo majorInfo = majorInfoMapper.getMajorInfoByMajorNumber(teacherInfo.getMajorNumber());
        DepartmentInfo departmentInfo = departmentInfoMapper.getDepartmentInfoByDepartmentNumber(majorInfo.getDepartmentNumber());
        return R.success(departmentInfo.getDepartmentName()+"-"+majorInfo.getMajorName());
    }

    /**
     * 教师第四页接口实现
     * 授课信息页
     * @param id
     * @return
     * //该页面展示教师所在专业即学院
     */
    @Override
    public R<TeachInfoDTO> teachInfo(Integer id)
    {
        TeacherInfo teacherInfo = teacherInfoMapper.getById(id);
        //拿到该教师所有的教学课程信息
        List<ScheduleInfo> scheduleInfos =  scheduleInfoMapper.getSchedulesByTeacherNumber(teacherInfo.getTeacherNumber());
        //先筛选出哪一个学期的课程信息最多
        getMax(scheduleInfos);
        TeachInfoDTO teachInfoDTO  = new TeachInfoDTO();
        //先存入学期名称
        SchoolCalendarInfo schoolCalendarInfo =  schoolCalendarInfoMapper.selectById(scheduleInfos.get(0).getCalendarId());
        String semesterName = "";
        if(schoolCalendarInfo.getSemester().equals("1"))
        {
            semesterName = (schoolCalendarInfo.getSchoolYear()+"第一学期");
        }
        else{
            semesterName =  (schoolCalendarInfo.getSchoolYear()+"第二学期");
        }
        teachInfoDTO.setSemesterName(semesterName);
        //遍历存入教学班名称、学生人数、课程门数和课程名称
        Integer studentCount =0;//学生人数
        Integer courseCount  = 0;//课程门数
        Integer lessonHours =0;//课时总数
        Set<String> classNames = new HashSet();//班级名称
        Set<String> courseNames = new HashSet<>();//课程名称
        for(int i=0;i<scheduleInfos.size();i++)
        {
            //班级名先用Set避免重复
           ClassInfo classInfo = classInfoMapper.getClassInfoByClassNumber(scheduleInfos.get(i).getClassNumber());
           classNames.add(classInfo.getClassName());
           //处理学生总人数
            studentCount+=studentInfoMapper.getStudentCountByClassNumber(classInfo.getClassNumber());

            CourseInfo courseInfo =  courseInfoMapper.getCourseInfoByCourseNumber(scheduleInfos.get(i).getCourseNumber());
           //课程名称用Set避免重复
               courseNames.add(courseInfo.getCourseName());//课程名称
           //课时总数计算
            lessonHours+=Integer.parseInt(courseInfo.getLessonHours());
        }
        List<String> className = new ArrayList<>(classNames);
        teachInfoDTO.setClassName(className);//班级名称
        List<String> courseName = new ArrayList<>(courseNames);
        teachInfoDTO.setCoursesName(courseName);//课程名称
        teachInfoDTO.setCourseCount(courseName.size());//课程门数
        teachInfoDTO.setStudentQuantity(studentCount);//学生总数
        teachInfoDTO.setLessonHours(lessonHours);//课时总数
        return R.success(teachInfoDTO);
    }

}
